package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.Message;
import com.xxx.entity.dto.MessageDto;
import com.xxx.service.IMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 消息通知表 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/message")
@Tag(name = "消息通知管理")
public class MessageController {

    @Autowired
    private IMessageService messageService;

    @PostMapping("/add")
    @Operation(summary = "添加消息")
    public Result add(@RequestBody Message message){
        Boolean flag = messageService.save(message);
        return flag ? Result.success("添加消息成功") : Result.error("添加消息失败");
    }

    @GetMapping("/delete/{messageId}")
    @Operation(summary = "删除消息")
    public Result delete(@PathVariable Integer messageId){
        Boolean flag = messageService.removeById(messageId);
        return flag ? Result.success("删除消息成功") : Result.error("不存在该消息Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改消息")
    public Result update(@RequestBody Message message){
        Boolean flag = messageService.updateById(message);
        return flag ? Result.success("修改消息成功") : Result.error("修改消息失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询消息")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
        IPage<Message> page = messageService.page(new Page<>(current,size));
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据消息Id、接收者Id、发送者Id条件查询消息")
    public Result selectByCondition(@RequestBody MessageDto messageDto){
        LambdaQueryWrapper<Message> lqw = new LambdaQueryWrapper<>();
        lqw.eq(messageDto.getReceiverId()!=null,Message::getReceiverId,messageDto.getReceiverId())
                .eq(messageDto.getSenderId()!=null,Message::getSenderId,messageDto.getSenderId())
                .eq(messageDto.getMessageId()!=null,Message::getMessageId,messageDto.getMessageId());
        IPage<Message> page = messageService.page(new Page<>(messageDto.getCurrent(), messageDto.getSize()),
                lqw);
        return Result.success(page);
    }
}

